#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    File Name:      time_utils
    Author:         'daze'
    date:           2018/10/23
"""
import datetime
import time

# 获取 10 位时间戳(s)
from AutoWool.main.utils.log_utils import Log


class TimeUtils:

    @staticmethod
    def get_cur_second_time_stamp() -> int:
        return int(time.time())

    # 13位时间戳(ms)
    @staticmethod
    def get_mills_time_stamp() -> int:
        return int(round(time.time() * 1000))

    # 13位时间戳转换成时间
    @staticmethod
    def timestamp2string(mills_time_stamp):
        try:
            d = datetime.datetime.fromtimestamp(mills_time_stamp)
            str1 = d.strftime("%Y-%m-%d %H:%M:%S.%f")
            # 2015-08-28 16:43:37.283000'
            return str1
        except Exception as e:
            Log.error(str(e))
            return ''

    @staticmethod
    def string2timestamp(str_value):
        try:
            d = datetime.datetime.strptime(str_value, "%Y-%m-%d %H:%M:%S")
            t = d.timetuple()
            time_stamp = int(time.mktime(t))
            time_stamp = float(str(time_stamp) + str("%06d" % d.microsecond)) / 1000000
            Log.log('string2timestamp timeStamp=' + str(time_stamp))
            return time_stamp
        except ValueError as e:
            Log.error(str(e))
            return ''
